package com.zkingsoft.web.service.impl;

import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.web.bean.SearchArticle;
import com.zkingsoft.web.bean.WebArticle;
import com.zkingsoft.web.bean.WebArticleType;
import com.zkingsoft.web.bean.WebImages;
import com.zkingsoft.web.dao.WebArticleDao;
import com.zkingsoft.web.dao.WebArticleTypeDao;
import com.zkingsoft.web.dao.WebImagesDao;
import com.zkingsoft.web.service.WebArticleService;
import com.zkingsoft.web.tool.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-12-01 10:36
 */
@Service("webArticleService")
public class WebArticleServiceImpl implements WebArticleService {

	@Autowired
	private WebArticleDao webArticleDao;
	@Autowired
	private WebImagesDao imagesDao;

	@Autowired
	private WebArticleTypeDao webArticleTypeDao;

	@Override
	public int add(WebArticle webArticle) {
		int i = webArticleDao.insert(webArticle);
		setImages(webArticle);
		return i;

	}

	/**
	 *  删除文章的照片后再重新添加
	 * 
	 * @param webArticle
	 */
	private void setImages(WebArticle webArticle) {
		WebImages del = new WebImages();
		del.setArtId(webArticle.getArtId());
		imagesDao.deleteByModel(del);
		List<WebImages> addList = new ArrayList<>();
		for (WebImages images : webArticle.getImages()) {
			if (!StringUtils.isBlank(images.getPath())) {
				images.setArtId(webArticle.getArtId());
				images.setTime(new Date());
				addList.add(images);
			}
		}
		if (!addList.isEmpty()) {
			imagesDao.batchInsert(addList);
		}
	}

	@Override
	public int batchAdd(List<WebArticle> webArticleList) {
		return webArticleDao.batchInsert(webArticleList);
	}

	@Override
	public int modifyByMap(WebArticle oldWebArticle, WebArticle newWebArticle) {

		try {
			if (!ModelUtils.isModified(oldWebArticle, newWebArticle)) {
				return 1;
			}
		} catch (NoSuchFieldException e1) {
			e1.printStackTrace();
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldWebArticle, newWebArticle);
		} catch (Exception e) {
			new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("artId", oldWebArticle.getArtId());
			webArticleDao.updateByMap(modifyMap);
		}
		setImages(newWebArticle);
		return 1;
	}

	@Override
	public int modifyByModel(WebArticle webArticle) {

		return webArticleDao.updateByModel(webArticle);

	}

	@Override
	public int remove(List<Long> list) {

		return webArticleDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long artId) {

		return webArticleDao.deleteById(artId);

	}

	@Override
	public int removeByModel(WebArticle webArticle) {

		return webArticleDao.deleteByModel(webArticle);

	}

	@Override
	public List<WebArticle> findInPage(WebArticle webArticle, PaginationVO pageVo) {

		return webArticleDao.selectInPage(webArticle, pageVo);

	}

	@Override
	public List<WebArticle> findByModel(WebArticle webArticle) {

		return webArticleDao.selectByModel(webArticle);

	}

	@Override
	public int findTotal(WebArticle webArticle) {

		return webArticleDao.selectTotalRecord(webArticle);

	}

	@Override
	public WebArticle findById(Long artId) {

		return webArticleDao.selectById(artId);

	}

	@Override
	public WebArticle findByTitle(String string) {
		WebArticle art = new WebArticle();
		art.setArtTitle(string);
		art.setCompanyId(Tools.getCompanyId());
		List<WebArticle> arts = webArticleDao.selectByModel(art);
		if (arts.size() > 0) {
			return arts.get(0);
		}
		return art;
	}

	@Override
	public List<WebArticle> findByTypeName(String string) {
		WebArticle art = new WebArticle();
		WebArticleType type = new WebArticleType();
		type.setArtTypeName(string);
		List<WebArticleType> types = webArticleTypeDao.selectByModel(type);
		if (types.size() > 0) {
			art.setArtTypeId(types.get(0).getArtTypeId());
		}
		return webArticleDao.selectByModel(art);
	}

	@Override
	public WebArticle findAround(Long artId, int i, String companyId) {

		return webArticleDao.selectAround(artId, i,companyId);
	}

	@Override
	public List<WebArticle> findNewArticle(String artBlog, PaginationVO pageVo) {
		if (pageVo.getOrder() == null) {
			pageVo.setSort("artCreatetiem");
			pageVo.setOrder("desc");
		}
		WebArticle art = new WebArticle();
		WebArticleType type = new WebArticleType();
		type.setCode(artBlog);

		List<WebArticleType> types = webArticleTypeDao.selectByModel(type);
		if (types.size() > 0) {
			art.setArtTypeId(types.get(0).getArtTypeId());
			WebUtil.setRequestAttribute("currentType", types.get(0));
		}
		art.setArtIsPublish(AppConstance.IS_Y);
		return webArticleDao.selectInPage(art, pageVo);
	}

	@Override
	public List<WebArticle> findByWord(String searchWord) {
		return webArticleDao.selectByWord(searchWord);
	}

	@Override
	public List<WebArticle> findByIds(String ids) {
		List<Long> idList = StringUtils.strToCollToLong(ids,",");

		return webArticleDao.selectByIds(idList,Tools.getCompanyId());
	}

	@Override
	public List<WebArticle> findBySearchModel(SearchArticle searchArticle, PaginationVO pageVo) {
		return webArticleDao.selectSearchInPage(searchArticle, pageVo);
	}

	@Override
	public int findTotalBySearchModel(SearchArticle searchArticle) {
		return webArticleDao.selectSearchTotal(searchArticle);
	}
}